package leetcode_301To600;

/**
 * 本代码来自 Cspiration，由 @Cspiration 提供
 * 题目来源：http://leetcode.com
 * - Cspiration 致力于在 CS 领域内帮助中国人找到工作，让更多海外国人受益
 * - 现有课程：Leetcode Java 版本视频讲解（1-900题）（上）（中）（下）三部
 * - 算法基础知识（上）（下）两部；题型技巧讲解（上）（下）两部
 * - 节省刷题时间，效率提高2-3倍，初学者轻松一天10题，入门者轻松一天20题
 * - 讲师：Edward Shi
 * - 官方网站：https://cspiration.com
 * - 版权所有，转发请注明出处
 */
public class _531_LonelyPixelI {

    /**
     * Given a picture consisting of black and white pixels, find the number of black lonely pixels.

     The picture is represented by a 2D char array consisting of 'B' and 'W',
     which means black and white pixels respectively.

     A black lonely pixel is character 'B' that located at a specific position
     where the same row and same column don't have any other black pixels.

     Example:

     Input:
     [['W', 'W', 'B'],
     ['W', 'B', 'W'],
     ['B', 'W', 'W']]

     Output: 3
     Explanation: All the three 'B's are black lonely pixels.

     time : O(m * n)
     space : O(n)
     * @param picture
     * @return
     */
    public int findLonelyPixel(char[][] picture) {
        int[] col = new int[picture[0].length];
        for (int i = 0; i < picture.length; i++) {
            for (int j = 0; j < picture[0].length; j++) {
                if (picture[i][j] == 'B') {
                    col[j]++;
                }
            }
        }

        int res = 0;
        for (int i = 0; i < picture.length; i++) {
            int count = 0;
            int pos = 0;
            for (int j = 0; j < picture[0].length; j++) {
                if (picture[i][j] == 'B') {
                    count++;
                    pos = j;
                }
                if (count == 1 && col[pos] == 1) {
                    res++;
                }
            }
        }

        return res;
    }
}
